﻿package Welsy4.Connect.commands
{
	import Welsy4.Connect.business.MeetingDelegate;
	import Welsy4.Connect.events.ChainEvent;
	import Welsy4.Connect.events.MeetingEvent;
	import Welsy4.Connect.events.ResponseEvent;
	import Welsy4.Connect.model.ConnectModel;
	import Welsy4.Connect.vo.HttpServiceHandler;
	import Welsy4.Connect.vo.IMeeting;
	import Welsy4.Connect.vo.Meeting;

	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.commands.SequenceCommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;

	import mx.controls.Alert;
	import mx.rpc.IResponder;


	public class UpdateMeetingCommand extends SequenceCommand implements Command, IResponder
	{

		override public function execute(event:CairngormEvent):void {


			ConnectModel.getInstance().addQueue("UpdateMeetingCommand.execute : " + ConnectModel.getInstance().lastMeetingFound.name + " ("+ ConnectModel.getInstance().lastMeetingFound.sco_id+ ")");

			var ev : MeetingEvent = MeetingEvent(event);												
			ConnectModel.getInstance().lastMeetingFound.Description = ev.meeting.Description;
			ConnectModel.getInstance().lastMeetingFound.Date_begin = ev.meeting.Date_begin;
			ConnectModel.getInstance().lastMeetingFound.Date_end = ev.meeting.Date_end;

			this.nextEvent = ChainEvent(event).nextChainedEvent;

			// UpdateMeetingCommand
			var meetingDelegate:MeetingDelegate = new MeetingDelegate(this);


			meetingDelegate.updateMeeting(ConnectModel.getInstance().lastMeetingFound);


		}

		public function result(data:Object):void {

			ConnectModel.getInstance().addQueue("UpdateMeetingCommand.result");

			ConnectModel.getInstance().httpServiceHandler = new HttpServiceHandler();
			ConnectModel.getInstance().httpServiceHandler.setXML(data.result);

			if (ConnectModel.getInstance().httpServiceHandler.getResultStatus() == ConnectModel.SESSION_OK) {
				var meeting:Meeting = new Meeting;
				Alert.show("Everything works ok!\n" + data.result);
				this.executeNextCommand();
				/*if (ConnectModel.getInstance().httpServiceHandler.xml.scos.sco.length() > 0) {

				   meeting.sco_id = ConnectModel.getInstance().httpServiceHandler.xml.scos.sco.attribute("sco-id");
				   meeting.name = ConnectModel.getInstance().httpServiceHandler.xml.scos.sco.name;

				   ConnectModel.getInstance().lastMeetingFound = meeting;

				   //Alert.show("meeting found: " + meeting.sco_id );

				   this.executeNextCommand();

				   } else {
				   CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent (ResponseEvent.ERROR, false, ResponseEvent.NO_DATA, data.result));
				   }
				 */
				//this.executeNextCommand();
			} else {
				Alert.show("Everything no works!\n" + data.result);
				CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent (ResponseEvent.ERROR, false, ResponseEvent.GENERIC, data.result));	
			}




		}

		public function fault(data:Object):void {
			ConnectModel.getInstance().addQueue("UpdateMeetingCommand.fault");
			CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent (ResponseEvent.ERROR, false, ResponseEvent.CONNECTION));
		}

	}

}

